<template>
  <div class="zy-content">
    <div class="searchBox search-box-layout">
      <Row :gutter="24">
        <Col span="4">
          <div>
            <p class="label">{{'牌号'}}</p>
            <div class="ipt">
              <Input
                type="text"
                v-model="params.customMark"
                clearable
                :placeholder="`${$t('common.inputInfo')}`"
                class="inputBorder"
              ></Input>
            </div>
          </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label">{{'牌号检验'}}</p>
            <div class="ipt">
              <Input
                type="text"
                v-model="params.customMarkCheck"
                clearable
                :placeholder="`${$t('common.inputInfo')}`"
                class="inputBorder"
              ></Input>
            </div>
          </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label hide-label">--</p>
            <div class="ipt">
              <Button
                class="search-btn"
                type="primary"
                @click="changeNum()"
              >{{ $t('common.Query') }}</Button>
              <Button
                class="reset-btn"
                @click="reset()"
                v-has="'dmsPurchaseList'"
              >{{ $t('common.reset') }}</Button>
            </div>
          </div>
        </Col>
      </Row>
    </div>
    <div class="list-layout mt16">
      <div class="btns clear">
        <span class="title">{{ $t('common.listInformation') }}</span>
        <Button @click="addHandle">{{'新增'}}</Button>
        <!-- <Button @click="importData">{{ $t('common.Import') }}</Button>
        <Button @click="downloadTemplate">{{ $t('common.Download') }}</Button>-->
        <Button
          @click="getTableColumns(columns)"
        >{{ handleContextMenuStatus ? $t('显示操作栏') : $t('隐藏操作栏') }}</Button>
      </div>
      <div class="tableContent">
        <Table
          ellipsis
          ref="selection"
          class="resizable-table"
          border
          @on-column-width-resize="onColumnWidthResize"
          :columns="columnsList"
          :data="list"
        >
          <template slot-scope="{ row }" slot="action">
            <span class="operationBtn" @click="editRow(row)">
              <i class="iconfont zy-common-bianji"></i>
              {{ $t('common.Edit')
              }}
            </span>
            <span class="operationBtn" @click="delRow(row)">
              <i class="iconfont zy-common-shanchu"></i>
              {{
              $t('common.Delete') }}
            </span>
          </template>
        </Table>
      </div>
      <div class="pageWrap mt16">
        <pageLeftTitle :total="total" />
        <Page
          :total="total"
          :page-size="params.pageSize"
          :current="params.pageNum"
          @on-change="changePageNum"
          @on-page-size-change="changePageSize"
          show-sizer
        />
      </div>
    </div>
    <Modal
      :mask-closable="false"
      v-model="deleteShow"
      :title="$t('common.Delete')"
      class="modal-title-layout"
    >
      <div class="icon red">
        <i class="iconfont zy-common-shanchu"></i>
      </div>
      <p class="title">
        <span>{{ $t('common.sureDelete') }}</span>
      </p>
      <div slot="footer">
        <Button type="default" @click="deleteShow = false">{{ $t('common.Cancle') }}</Button>
        <Button type="primary" @click="deleteSure">{{ $t('maintenance.Confirmed') }}</Button>
      </div>
    </Modal>

    <Modal
      :mask-closable="false"
      v-model="createStatus"
      :title="form.id ? $t('common.Edit') : $t('common.New')"
      class="modal-content-layout"
    >
      <Form
        v-if="createStatus"
        class="formList"
        :model="form"
        label-position="top"
        ref="form"
        :rules="rules"
      >
        <FormItem :label="$t('客户牌号')" required prop="customMark">
          <Input
            type="text"
            v-model="form.customMark"
            :placeholder="`${$t('common.inputInfo')}`"
            class="inputBorder"
          ></Input>
          <!-- <Select v-model="form.code" filterable clearable
                  :placeholder="$t('common.selectInfo')">
            <Option v-for="(item, i) in dictName"
                    :value="item"
                    :label="item"
                    :key="i">
            </Option>
          </Select>-->
        </FormItem>
        <FormItem :label="$t('客户牌号检验')" required prop="customMarkCheck">
          <Input
            type="text"
            v-model="form.customMarkCheck"
            :placeholder="`${$t('common.inputInfo')}`"
            class="inputBorder"
          ></Input>
        </FormItem>
      </Form>
      <div slot="footer" v-if="createStatus">
        <Button type="default" @click="createStatus = false">{{ $t('common.Cancle') }}</Button>
        <Button type="primary" @click="addHandleSubmit('form')">{{ $t('maintenance.Confirmed') }}</Button>
      </div>
    </Modal>

    <importExcelModal
      :importExcelStateF="importExcelState"
      v-if="importExcelState"
      @importExcelChange="importExcelChange"
    ></importExcelModal>
  </div>
</template>

<script>
import ImportExcelModal from "@/components/importExcel.vue";
import fileDownload from "js-file-download";
import toggleTableOperation from "@/mixins/toggleTableOperation";
export default {
  name: "list.vue",
  components: { ImportExcelModal },
  mixins: [toggleTableOperation],
  data() {
    return {
      params: {
        pageSize: 10,
        pageNum: 1,
        customMark: "",
        customMarkCheck: ""
      },
      // dictName: ['故障分类', '故障类型','故障现象', '设备类别', '备件分类', '维修工时单价', '技能类型', '技能', '维保达成率目标值', 'COT目标值', '维保成本单价','CAPEX FACTORY',],
      columns: [
        {
          title: "客户牌号",
          key: "customMark",
          align: "center",
          resizable: true
        },
        {
          title: "客户牌号检验",
          key: "customMarkCheck",
          align: "center",
          resizable: true
        },
        {
          title: "创建人",
          key: "createBy",
          align: "center",
          resizable: true
        },
        {
          title: "创建时间",
          key: "createTime",
          align: "center",
          resizable: true
        },
        {
          title: "修改人",
          key: "updateBy",
          align: "center",
          resizable: true
        },
        {
          title: "修改时间",
          key: "updateTime",
          align: "center",
          resizable: true
        },
        {
          title: `${this.$t("common.operation")}`,
          slot: "action",
          width: 350,
          // width: this.util.autoWidth(450, 600),
          align: "center",
          fixed: "right"
        }
      ],
      importExcelState: false,
      list: [],
      total: 0,
      deleteShow: false,
      currentItem: {},
      createStatus: false,
      form: {},
      nameList: [],
      rules: {
        customMark: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur"
          }
        ],
        customMarkCheck: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getTableColumnsWidth(this.columns);
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleRemote(query) {
      if (query) {
        this.axios({
          method: "get",
          url: "/dms/dict/nameList",
          params: {
            name: query
          }
        }).then(res => {
          if (res.code === 200) {
            this.nameList = res.data;
          } else {
            this.$Message.warning({
              content: res.msg,
              duration: 6,
              closable: true
            });
          }
        });
      } else {
        // this.nameList = []
      }
    },
    importExcelChange(val) {
      this.importExcelState = false;
      if (val === "success") {
        this.$Message.success("导入成功!");
        this.changeNum();
      }
    },
    getList() {
      this.axios({
        method: "post",
        url: "/dms/CustomMarkOperate/queryCustomMarkList",
        data: {
          ...this.params
        }
      }).then(res => {
        if (res.code === 200) {
          this.list = res.data.records;
          this.total = res.data.total;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    reset() {
      this.params = {};
      this.changeNum();
    },
    changeNum() {
      this.params.pageNum = 1;
      this.params.pageSize = 10;
      this.getList();
    },
    addHandle() {
      this.form = {};
      this.createStatus = true;
    },
    downloadTemplate() {
      const filName = "数据字典导入模板";
      this.axios({
        method: "get",
        url: "/dms/dict/exportTemplate",
        responseType: "blob"
      }).then(res => {
        fileDownload(res, filName + ".xlsx");
      });
    },
    importData() {
      this.importExcelState = true;
    },
    addHandleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.axios({
            method: "post",
            url: this.form.id
              ? "/dms/CustomMarkOperate/updateCustomMark"
              : "/dms/CustomMarkOperate/insertCustomMark",
            data: {
              ...this.form
            }
          }).then(res => {
            this.$Message.success(res.msg);
            this.currentItem = {};
            this.createStatus = false;
            this.getList();
          });
        }
      });
    },
    editRow(row) {
      const { id, customMark, customMarkCheck } = row;
      this.form = { id, customMark, customMarkCheck };
      this.createStatus = true;
    },
    delRow(row) {
      this.currentItem = row;
      this.deleteShow = true;
    },
    deleteSure() {
      this.axios({
        method: "get",
        url: "/dms/CustomMarkOperate/deleteCustomMark?id=" + this.currentItem.id
        // data: [this.currentItem.id]
      }).then(res => {
        if (res.code === 200) {
          this.$Message.success(res.msg);
          this.currentItem = {};
          this.deleteShow = false;
          this.getList();
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true
          });
        }
      });
    },
    changePageNum(val) {
      this.params.pageNum = val;
      this.getList();
    },
    changePageSize(val) {
      this.params.pageSize = val;
      this.getList();
    }
  }
};
</script>

<style scoped></style>
